<template>
  <a-modal
    title="编辑物料清单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form :form="form">
          <a-form-item
            label="父项产品"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <pro-select
              placeholder="请选择父项产品"
              v-decorator="['parentId', { rules: [{ message: '请选择父项产品！' }]}]"
            />
          </a-form-item>
          <a-form-item
            label="子项产品"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
          >
            <pro-select
              placeholder="请选择子项产品"
              v-decorator="['subitemId', { rules: [{ message: '请选择子项产品！' }]}]"
            />
          </a-form-item>
          <a-form-item
            label="单位用量"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback>
            <a-input-number
              style="width: 100%"
              :min="1"
              v-decorator="['unitCon', {rules: [{required: true, message: '请输入单位用量'}]}]" />
          </a-form-item>
          <a-form-item
            label="备注"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback
          >
            <a-textarea placeholder="请输入备注" v-decorator="['remarks']" :auto-size="{ minRows: 3, maxRows: 6 }"/>
          </a-form-item>
        </a-form>
      </a-form></a-spin>
  </a-modal>
</template>

<script>
import { bomEdit } from '@/api/modular/main/bom/bomManage'
import ProSelect from '@/components/MyComp/ProSelect'
export default {
  components: {
    ProSelect
  },
  data () {
    return {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 15 }
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    // 初始化方法
    edit (record) {
      this.visible = true
      setTimeout(() => {
        this.form.setFieldsValue(
          {
            id: record.id,
            parentId: record.parentId,
            subitemId: record.subitemId,
            remarks: record.remarks,
            unitCon: record.unitCon
          }
        )
      }, 100)
    },
    handleSubmit () {
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          for (const key in values) {
            if (typeof (values[key]) === 'object' && values[key] != null) {
              values[key] = JSON.stringify(values[key])
            }
          }
          bomEdit(values).then((res) => {
            if (res.success) {
              this.$message.success('编辑成功')
              this.confirmLoading = false
              this.$emit('ok', values)
              this.handleCancel()
            } else {
              this.$message.error('编辑失败' + res.message)//  + res.message
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
